
#include <stdio.h>
#include <map>
#include <string>
#include <iostream>
#include <utility>
#include <string.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/socket.h>
#include "forkServer.h"
#include <string>
using namespace std;
// include stuff...






int ChangeDirectory(char* dir )
{
    chdir(dir);
    return 0;
}

// TODO: Implement all needed functions

int Delete(char* dir)
{
    return rmdir(dir);
}

// get file
FILE getFile(char* dir)
{
    FILE  *fp = fopen(dir, "r");
    if(fp == NULL)        
            {
                printf("ERROR: File %s not found.\n", dir);                
            }
    
    return *fp;
}

char* listDirectory(char* dir)
{
    char* returnString = new char[BUFSIZE];
        
    // first off, we need to create a pointer to a directory
    DIR *pdir = NULL; // remember, it's good practice to initialise a pointer to NULL!
    pdir = opendir ("."); // "." will refer to the current directory
    struct dirent *pent = NULL;

    // I used the current directory, since this is one which will apply to anyone reading
    // this tutorial~ If I said "C:\\" and you're on Linux, it may get a little confusing!
    if (pdir == NULL) // if pdir wasn't initialised correctly
    { // print an error message and exit the program
       strcat(returnString,"\nERROR! pdir could not be initialised correctly");
        
    } // end if

    while (pent = readdir (pdir)) // while there is still something in the directory to list
    {
        if (pent == NULL) // if pent has not been initialised correctly
        { // print an error message, and exit the program
             strcat(returnString,"\nERROR! pent could not be initialised correctly");
     
        }
        // otherwise, it was initialised correctly. Let's print it on the console:
        strcat(returnString,pent->d_name);
        strcat(returnString,"\n");
        
    }

    // finally, let's close the directory
    closedir (pdir);

    
    return returnString; // everything went OK
    
}


int addFile(int socket,char* dir)
{
    char buffer[100];
FILE *fp;
read(socket,buffer,100);
fp=fopen(dir,"w");
fprintf(fp,"%s",buffer);
}


int fileManager_TCP(int socket,char* command)
{       
    int nReturn = 0;
    char* dir = new char[BUFSIZE];
    
    if(strcmp(command,"ls") == 0)
    {
         read(socket, dir, BUFSIZE);
         nReturn = send(socket,listDirectory(dir) , BUFSIZE, 0);
    }
    else if(strcmp(command,"put") == 0)
    {
        read(socket, dir, BUFSIZE);
        nReturn = addFile(socket,dir);
    }
    else if(strcmp(command,"get") == 0)
    {
    }
    else if(strcmp(command,"cd") == 0)
    {
        nReturn = ChangeDirectory(dir);
    }
    else if(strcmp(command,"cwd") == 0)
    {
        char cwd[256];
        getcwd(cwd, sizeof(cwd));
        send(socket,cwd , sizeof(cwd), 0);                
    }
    else
    {
    }
 
            
            
    return nReturn;   
}